﻿using System.Net;
using System.Web.Mvc;
using StructureMap;
using WS.EKA.DI;
using WS.EKA.Utilities.Consts;
using WS.EKA.Utilities.Core;
using WS.EKA.Utilities.Extensions;
namespace WS.EKA.Portal.Controllers
{
    public class HomeController : Controller
    {
        [ViewportMetaDataAttribute]
        public ActionResult Index()
        {
            var isLegalDevice = AuthencateDevice();
            if (!isLegalDevice)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }
            return View();
        }
       
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var viewportAttributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(ViewportMetaDataAttribute), false);
            if (viewportAttributes.Length == 1)
            {
                var screenAdapter = ObjectFactory.GetInstance<IScreenAdapter>();
                screenAdapter.SetViewport(s => ViewBag.Viewport = s);
            }

            base.OnActionExecuting(filterContext);
        }

        private bool AuthencateDevice()
        {
            var currentMode = RegistryModeFactory.GetCurrentMode();
            if (currentMode == RegistryMode.Debug)
            {
                return true;
            }
            var headers = Request.Headers;
            if (headers==null)
            {
                return false;
            }
            var clientAuthencationString = headers.Get(UserConsts.WSMobile);
            if (UserConsts.WSMobile.ConfigValue().EqualsOrdinalIgnoreCase(clientAuthencationString))
            {
                return true;
            }
            return false;
        }

    }
}
